package lesson3.slides.net;

import java.io.*;
import java.net.*;

public class ServerSocketExample {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(8099);
        System.out.println("Server started: " + server);
        try {
            Socket connection = server.accept();
            try {
                System.out.println("Connection established: " + connection);
                BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                PrintWriter output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())), true);
                while (true) {
                    String s = input.readLine();
                    if (s.equals("quit")) {
                        break;
                    }
                    System.out.println("Client said: " + s);
                    output.println("You said " + s);
                }

            } finally {
                System.out.println("Closing connection...");
                connection.close();
            }
        } finally {
            System.out.println("Server shutdown...");
            server.close();
        }
    } //end main  
} //end class
